package ${packPath }.service.impl;

import java.util.List;

import com.apache.api.vo.ParamsVo;
import com.apache.cache.util.Validator;
import com.apache.database.db.IDao;
import com.apache.database.model.MethodParam;
import com.apache.database.model.Page;
import com.apache.exception.BusinessException;
import ${packPath}.entity.${sort.objName };
import ${packPath}.manager.${sort.objName }Manager;

public class ${sort.objName}ManagerImpl implements ${sort.objName}Manager {

	protected IDao ${beanName}Dao;
	protected final String entityName = "${packPath}.entity.${sort.objName}";

	public void set${sort.objName}Dao(IDao ${beanName}Dao) {
		this.${beanName}Dao = ${beanName}Dao;
	}

	public String saveInfo(ParamsVo<${sort.objName}> vo) throws BusinessException {
		${sort.objName} ${beanName} = vo.getObj();
		String cacheKey = "";
		//String cacheKey="${beanName}_"+infoId;
		if(Validator.isNull(${beanName}.get${PrimaryAttr}())){
		    String infoId = Validator.generate();
		    //定义对象缓存KEY,如果不需要缓存对象请不要对变量赋值，如果要缓存对象建议使用infoId
		   ${beanName}.set${PrimaryAttr}(infoId);
		}
		MethodParam param = new MethodParam("${sort.objName}", cacheKey, "", entityName);
		param.setVaule(${beanName});
		if (${beanName}Dao.insert(param)) {
			return ${beanName}.get${PrimaryAttr}();
		}
		return "";
	}

	public boolean editInfo(ParamsVo<${sort.objName}> vo) throws BusinessException {
		${sort.objName} ${beanName} = vo.getObj();
		if (Validator.isNotNull(${beanName}.get${PrimaryAttr}())) {
			String cacheKey = "";
			//String cacheKey="${beanName}_"+${beanName}.get${PrimaryAttr}();
			MethodParam param = new MethodParam("${sort.objName}", cacheKey, "", entityName);
			param.setVaule(${beanName});
			return ${beanName}Dao.edit(param);
		}
		return false;
	}

	public boolean deleteInfo(ParamsVo<${sort.objName}> vo) throws BusinessException {
		String infoId = vo.getInfoId();
		if (Validator.isNull(infoId)) {
			return false;
		}
		String cacheKey = "";
		//String cacheKey="${beanName}_"+infoId;
		String mark = Validator.getDefaultStr(String.valueOf(vo.getParams("isDelete")), "true");
		MethodParam param = new MethodParam("ById", cacheKey, "", entityName);
		param.setInfoId(infoId);
		${sort.objName} info = (${sort.objName}) ${beanName}Dao.selectById(param);
		if (Validator.isEmpty(info)) {
			return false;
		}
		param.setVaule(info);//此处需要先将状态值赋值为删除状态
		if ("false".equals(mark)) {//逻辑删除
			param.setKey("${sort.objName}");
			return ${beanName}Dao.edit(param);
		} else {
			param.setParams("${PrimaryId}", infoId);
			param.setDelete(true);
			return ${beanName}Dao.delete(param);
		}
	}

	public Object getInfoById(ParamsVo<${sort.objName}> vo) {
		String infoId = vo.getInfoId();
		String cacheKey = "";
		//String cacheKey="${beanName}_"+infoId;
		if (Validator.isNull(infoId)) {
			return null;
		}
		MethodParam param = new MethodParam("ById", cacheKey, "", entityName);
		param.setInfoId(infoId);
		return ${beanName}Dao.selectById(param);
	}

	public Object execute(ParamsVo<${sort.objName}> vo) {
		// TODO Auto-generated method stub
		return null;
	}

	public Page getPageInfo(ParamsVo<${sort.objName}> vo) {
		MethodParam param = setMethodParams(vo, 2);
		int pageSize = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageSize")), "100"));
		int pageIndex = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageIndex")), "1"));
		param.setPageIndex(pageIndex);
		param.setPageSize(pageSize);
		//param.setParams("orderBy", "create_Time desc");
		Page page = ${beanName}Dao.pageSelect(param);
		return page;
	}

	public List<${sort.objName}> getList(ParamsVo<${sort.objName}> vo) {
		MethodParam param = setMethodParams(vo, 2);
		return ${beanName}Dao.select(param);
	}

	public long countInfo(ParamsVo<${sort.objName}> vo) {
		MethodParam param = setMethodParams(vo, 1);
		return ${beanName}Dao.count(param);
	}

	private MethodParam setMethodParams(ParamsVo<${sort.objName}> vo, int type) {
		String methodKey = Validator.getDefaultStr(vo.getMethodKey(), "ByProperty");
		StringBuffer sb = new StringBuffer(${beanName}Dao.getSql(type));
		MethodParam param = new MethodParam(methodKey, "", "", entityName);
		${sort.objName} ${beanName} = vo.getObj();
		if(!Validator.isEmpty(${beanName})){
	<#list metadatas as mt><#if mt.dataType=="4" || mt.dataType=="5">
	    if(Validator.isNotNull(${beanName}.get${upper(mt.dataAttr)}())){
	        sb.append(" and ${mt.dataAttr} = :${mt.dataAttr}");
			param.setParams("${mt.dataAttr}", ${beanName}.get${upper(mt.dataAttr)}());
	    }
       </#if></#list>
        }
		param.setSqlStr(sb.toString());
		return param;
	}
}
